package com.lijy.likebutton.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.Toast
import androidx.core.view.marginTop
import com.lijy.likebutton.R
import com.lijy.likebutton.util.DimenUtils

/**
 *@time:     2020/12/8 0008
 *@author:   Lijy

 *@description:
 */
class ThumbUpView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr), View.OnClickListener {

    private var thumbsView: ThumbsView
    private var countView: CountView

    //按钮状态
    var mIsSelectedFlag = false
    var mLikeNum = 0
    var mTextColor = Color.parseColor("#cccccc")

    init {

        val obtainStyledAttributes = context.obtainStyledAttributes(attrs, R.styleable.ThumbUpView)
        mIsSelectedFlag = obtainStyledAttributes.getBoolean(R.styleable.ThumbUpView_is_like, mIsSelectedFlag)
        mLikeNum = obtainStyledAttributes.getInt(R.styleable.ThumbUpView_like_num, mLikeNum)
        mTextColor = obtainStyledAttributes.getColor(R.styleable.ThumbUpView_text_color, mTextColor)
//        textSize = obtainStyledAttributes.getInt(R.styleable.ThumbUpView_text_size, textSize)

        thumbsView = ThumbsView(mIsSelectedFlag, context)
        countView = CountView(context, mLikeNum, mTextColor)


        //布局
        onMyLayout()
        setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        if(mLikeNum == 0 && mIsSelectedFlag){
            Toast.makeText(context, "在点就负数了", Toast.LENGTH_SHORT).show()
           return
        }
        mIsSelectedFlag = !mIsSelectedFlag
        thumbsView.mIsSelectedFlag = mIsSelectedFlag
        if (mIsSelectedFlag) {
            //喜欢
            mLikeNum++
            countView.calculateChangeText(1)
        } else {
            //取消喜欢
            mLikeNum--
            countView.calculateChangeText(-1)
        }
        thumbsView.startAnim()
    }

    //布局 并设置初始状态
    private fun onMyLayout() {
        //icon布局
        val thumbsViewLP =
            LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        thumbsViewLP.topMargin = paddingTop
        thumbsViewLP.leftMargin = paddingLeft
        thumbsViewLP.bottomMargin = paddingBottom
        addView(thumbsView, thumbsViewLP)
        //记数布局
        val countViewLP =
            LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        countViewLP.topMargin = paddingTop
        countViewLP.rightMargin = paddingRight
        countViewLP.bottomMargin = paddingBottom
        addView(countView, countViewLP)
    }

    /*设置参数*/
    fun setIsSelectedFlag(isSelectedFlag: Boolean): ThumbUpView{
        this.mIsSelectedFlag = isSelectedFlag
        thumbsView.mIsSelectedFlag = isSelectedFlag
        return this
    }

    fun setLikeNum(likeNum: Int): ThumbUpView{
        this.mLikeNum = likeNum
        countView.setLikeNum(likeNum)
        return this
    }

    fun setTextColor(textColor: Int): ThumbUpView{
        this.mTextColor = textColor
        countView.setTextColor(textColor)
        return this
    }
}